var app = angular.module('ZJPC');
app.controller('SearchController', ['$scope','$state','$rootScope','$timeout','$location','DataService',SearchController]);

function SearchController ($scope,$state,$rootScope,$timeout,$location,DataService) {
	
	$scope.foldpullshow = false;
	$scope.foldpull = false;
	$scope.checkkey = function(e){
		var keycode = window.event?e.keyCode:e.which;
        if(keycode==13&&$scope.search_entry!=''){
            $scope.searchIt()
       	}
	}

	$scope.clearInput = function(e){
		$scope.search_entry = '';
		$scope.foldpullshow = false;
		$scope.li = null;
		$scope.showSerachList = false;
		$scope.searchList = [];
		$scope.search_entry = ''
		$scope.showNoResult = false;
		$scope.search_entry1 = '';
		$scope.foldpullshow = false;
		$state.go('app.region.main.search')
	}
	$scope.clearInput();

	$scope.li = null;

	$scope.searchIt = function(r){
		//计算search调取哪个接口
/*		var re = /^[0-9]+.?[0-9]*$/; //数字*/
		var re2 = /(^[\u4E00-\u9FA5]{1}[A-Z0-9]{6}$)|(^[A-Z]{2}[A-Z0-9]{2}[A-Z0-9\u4E00-\u9FA5]{1}[A-Z0-9]{4}$)|(^[\u4E00-\u9FA5]{1}[A-Z0-9]{5}[挂学警军港澳]{1}$)|(^[A-Z]{2}[0-9]{5}$)|(^(08|38){1}[A-Z0-9]{4}[A-Z0-9挂学警军港澳]{1}$)/
		var dataGroup = {};
		//分辨是车牌还是查询人
		if($scope.search_entry==''){
			
		}else if(re2.test($scope.search_entry)==true){
			DataService.getCar($scope.search_entry,function(data){
				//车辆的与人不同，优先选择卡口数据，所以第一步只需要判断是否有卡口数据
				if($scope.li!=null){
					//是从卡口列表进来的
					dataGroup = $scope.li;
					if(data.length>0){
						dataGroup['type'] = 'incar'
					}else{
						dataGroup['type']='outcar';
					}
					dataGroup['li'] = null;
					$scope.li = null;
					$scope.choice= false;
					$scope.foldpullshow = true;
					$state.go('app.region.main.search.cardetail.track',{data:dataGroup})
				}else if(data!=null&&data.length!=0){
					//直接查询时，判断是否查询到值
					dataGroup = data[0];
					dataGroup['type'] = 'incar'
					dataGroup['li'] = null;
					$scope.choice= false;
					$scope.foldpullshow = true;
					$state.go('app.region.main.search.cardetail.track',{data:dataGroup})	
				}else{
					$scope.li = null;
					$scope.showNoResult = true;
					$scope.search_entry1 = $scope.search_entry;
				}
			})
		}else if($scope.search_entry.length<8){
			//代表是人
			DataService.getPerson($scope.search_entry,function(data){
			//人的情况下，有可能出现多个值，因此先判断data的个数
				//查不到值
				console.log(data)
				if(data==null||data.length==0){
					//来源为列表，直接选择列表的数据填进去
					if($scope.li!=null&&$scope.li.jid!=undefined){
						dataGroup =  cloneObj($scope.li)
						dataGroup['type']='outer'
						$scope.li = {}
						$scope.choice = false;
						$scope.foldpullshow = true;
						console.log(1,data)
						$state.go('app.region.main.search.persondetail.track',{data:dataGroup})
					}else{
						//查不到值
						$scope.li = {}
						$scope.showNoResult = true;
						$scope.search_entry1 = $scope.search_entry;
					}
				}else if(data.jid!=undefined){
					//一个值，直接进入下一步,同时为了取用里面的图片将li带入;非列表时传入一个null
					dataGroup = data;
					dataGroup['li'] = $scope.li;
					$scope.li = {}
					$scope.foldpullshow = true;
					$scope.choice = false;
					dataGroup['type'] = dataGroup.rylx=='学生'?'student':'teacher'
					$state.go('app.region.main.search.persondetail.track',{data:dataGroup})
				}else if(data.length==1){
					//查询姓名情况的单个值，直接进入下一步,同时为了取用里面的图片将li带入;非列表时传入一个null
					dataGroup = data[0];
					dataGroup['li'] = $scope.li;
					$scope.li = {}
					$scope.foldpullshow = true;
					$scope.choice = false;
					dataGroup['type'] = dataGroup.rylx=='学生'?'student':'teacher'
					$state.go('app.region.main.search.persondetail.track',{data:dataGroup})
				}else{
					//查询到多个值的情况，此时将显示一个界面，进行下一步判断
					$scope.searchList = data;
					$scope.showSerachList = true;
					$scope.choice = false;
				}
			})
		
		}else{
			DataService.getPerson($scope.search_entry,function(data){
				dataGroup = $scope.li;
				dataGroup['xm'] = $scope.li['name'];
				dataGroup['type'] =  'outer'
				$state.go('app.region.main.search.persondetail.track',{data:dataGroup})		
			})

			/*$state.go('app.region.main.search.persondetail.track',{data:dataGroup})*/
			//以后加入mac地址等查询
		}
		//

/*		var result = {type:''}
		if($scope.search_entry.length==6&&re.test($scope.search_entry)){
			result = {type:'teacher'}
			DataService.getPerson($scope.search_entry,callback)
		}else if($scope.search_entry.length==7&&re.test($scope.search_entry)){
			result = {type:'student'}
			DataService.getPerson($scope.search_entry,callback)
		}else if(re2.test($scope.search_entry)){
			result = {type:'car'}
			DataService.getCar($scope.search_entry,callback)
		}else{
			alert("请输入完整正确的工号、学号或车牌号进行查询。")
		}
*/
	}
/*	function callback(result,data){
		console.log(111,data)
		var dataGroup = {}
		if(data==undefined&&result.type=='car'){
			dataGroup = $scope.li;
			dataGroup['type']='car';
			$state.go('app.region.main.search.cardetail.track',{data:dataGroup})
		}else if(data==undefined&&(result.type=='student'||result.type=='teacher')){
			dataGroup = $scope.li;
			dataGroup['type']='outer'
			$state.go('app.region.main.search.persondetail.track',{data:dataGroup})
		}else if(result.type=='car'){
			dataGroup = data;
			dataGroup['type']='car'
			dataGroup['li'] = $scope.li;
			$state.go('app.region.main.search.cardetail.track',{data:dataGroup})
		}else if(result.type=='teacher'){
			dataGroup = data;
			dataGroup['li'] = $scope.li;
			dataGroup['type']='teacher'
			$state.go('app.region.main.search.persondetail.track',{data:dataGroup})
		}else if(result.type=='student'){
			dataGroup = data;
			dataGroup['li'] = $scope.li;
			dataGroup['type'] = 'student';
			console.log(dataGroup)
			$state.go('app.region.main.search.persondetail.track',{data:dataGroup})
		}else{
			alert("未查询到信息，请输入正确的工号、学号或车牌号")
		}
		$scope.li = {}
	}*/
	//
	//
	$scope.clickSearchList = function(li){
		var dataGroup = li;
		dataGroup['li'] = $scope.li;
		$scope.li = {}
		dataGroup['type'] = dataGroup.rylx=='学生'?'student':'teacher'
		$scope.showSerachList = false;
		$scope.foldpullshow = true;
		$state.go('app.region.main.search.persondetail.track',{data:dataGroup})
	}


	//选项板控制
	$scope.choice = false;

	$scope.iii = $location.path();
	$scope.showChoice = function(e){
		if($location.path()=='/app/region/main/search'){
			var timePromise = $timeout(function () {
			 	if(e=='show'){
					$scope.choice = true;
				}else{
					$scope.choice = false;
				}
			}, 100);
		}else{
			$scope.choice = false;
		}	
	}

	$scope.clickPanel = function(url){
		$scope.foldpullshow = true;
		$state.go(url)
		$scope.choice = false;
	}

	//列表的情况
	$scope.$on('detail',function(e,m){
		$scope.search_entry = m.name;
		$scope.searchIt(m.type)
		$scope.li = m.li;
	})

	$scope.$on('clear',function(e,m){
		$scope.li = null;
		$scope.showSerachList = false;
		$scope.searchList = [];
		$scope.search_entry = ''
		$scope.showNoResult = false;
		$scope.search_entry1 = '';
		$scope.foldpullshow = false;
	})

	$scope.$watch('search_entry',function(newValue,oldValue){
		if(newValue!=oldValue){
			$scope.showSerachList = false;
			$scope.showNoResult = false;
			$scope.search_entry1 = '';
		}
		if(oldValue.length!=0&&newValue.length==0){
			$scope.showSerachList = false;
			$scope.showNoResult = false;
			$scope.search_entry1 = '';
		}
	})

	var cloneObj = function(obj){
    var str, newobj = obj.constructor === Array ? [] : {};
    if(typeof obj !== 'object'){
        return;
    } else if(window.JSON){
        str = JSON.stringify(obj), //系列化对象
        newobj = JSON.parse(str); //还原
    } else {
        for(var i in obj){
            newobj[i] = typeof obj[i] === 'object' ? 
            cloneObj(obj[i]) : obj[i]; 
        }
    }
    return newobj;
};

}


